package com.java.exer_blackjack;

import javax.swing.*;
import java.util.Arrays;

/**
 * @author shkstart
 * @create 2021-01-13 15:18
 */
public class RunGame {
    private static Player mine;
    private static Player computer;
    private static Poker[] pokers;

    //发牌的指针
    private static int pokerIndex = 0;

    //确定电脑要牌的上线
    private static int FINAL_MATH = 18;


    public static void main(String[] args) {
        //初始化游戏
        initGame();

        //游戏界面绘制
        viewGame();
    }

    private static void viewGame() {
        boolean flag = true;
        while (flag) {
            String info = "-------21点游戏------\n" +
                    computer + "\n" + mine.getInfo() + "\n" + "1-要牌 \t 2-停牌";
            String input = JOptionPane.showInputDialog(info);
            if (input == null || input.trim().isEmpty()) {
                JOptionPane.showMessageDialog(null, "请规范输入");
                continue;
            }
            switch (input.trim().charAt(0)) {
                case '1':
                    mine.getPoker(pokers[pokerIndex++]);
                    if (computer.getPoint() < FINAL_MATH) {
                        computer.getPoker(pokers[pokerIndex++]);
                    }
                    break;
                case '2':
                    while (computer.getPoint() < FINAL_MATH) {
                        computer.getPoker(pokers[pokerIndex++]);
                    }
                    boolean isflag = true;
                    if (computer.getPoint() == 21) {
                        isflag = false;
                    } else if (mine.getPoint() > 21) {
                        isflag = false;
                    } else if (computer.getPoint() < 21) {
                        if (computer.getPoint() > mine.getPoint()) {
                            isflag = false;
                        }
                    }
                    info = "--------恭喜你" + (isflag ? "赢了" : "输了") + "----- \n" +
                            computer.getInfo() + " :" + computer.getPoint() + "\n" +
                            mine.getInfo() + " :" + mine.getPoint();
                    JOptionPane.showMessageDialog(null, info);
                    flag = false;
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "垃圾再来呀");
                    continue;
            }

        }

    }

    private static void initGame() {
        //初始化牌
        initPokers();

        //输入玩家姓名
        String playName = JOptionPane.showInputDialog("请输入您的名字");
        if (playName == null || playName.trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "名字格式错误");
//            playName="wwww";
        }
        //将起好的名字 作为参数传到mine 中
        mine = new Player(playName);

        computer = new Player("东方求败");

        //先给电脑和玩家每人两张牌
//      computer.getPoker(pokers[pokerIndex++]);
        mine.getPoker(pokers[pokerIndex++]);
        computer.getPoker(pokers[pokerIndex++]);
        mine.getPoker(pokers[pokerIndex++]);

    }

    private static void initPokers() {
        pokers = new Poker[52];

        for (int i = 0; i < 4; i++) {
            for (int j = 1; j < 14; j++) {
                int index = i * 13 + j - 1;
                pokers[index] = new Poker(i, j);

            }
        }

//        System.out.println(Arrays.toString(pokers));

        Arrays.sort(pokers);

//        System.out.println(Arrays.toString(pokers));

    }
}
